package com.adinnet.api;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;

/**
 * MyBatis-plus 代码生成器
 * Created by wangfan on 2019-02-11 上午 9:50.
 */
public class MybatisGenerator {

    private static String url="jdbc:mysql://10.40.254.78:3306/zhiboyuan";
    private static String driver="com.mysql.cj.jdbc.Driver";
    private static String userName="root";
    private static String password="Adinnet2019#";


    private static String outputDir="d:\\temp"; // 输出位置
    private static String author="liukp";       //作者

    public static void main(String[] args) {
        String packageName = "com.adinnet.api.appVersion";
        String[] tableNames = {"t_app_version"};
        boolean serviceNameStartWithI = false; // user -> UserService, 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName, tableNames);
    }

    public static void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        AutoGenerator mpg = new AutoGenerator();  // 代码生成器

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outputDir);  // 输出位置
        gc.setFileOverride(true);  // 覆盖文件
        gc.setOpen(true);
        gc.setAuthor(author);
        gc.setActiveRecord(false);  // ActiveRecord 模式
        gc.setEnableCache(false);  // 二级缓存
        gc.setIdType(IdType.AUTO);          //主键自增
        gc.setDateType(DateType.ONLY_DATE); // 时间类型 Date
        if (!serviceNameStartWithI) {
            gc.setServiceName("%sService");  // service命名方式
        }
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName(driver);
        dsc.setUsername(userName);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageName);
        pc.setMapper("mapper");
        pc.setXml("xml");
        pc.setController("controller");
        pc.setEntity("model");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setCapitalMode(true);  // 大写命名
        strategy.setEntityLombokModel(true); // lombok模型
        strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 表名映射策略
        strategy.setNaming(NamingStrategy.underline_to_camel);  // 字段映射策略
        strategy.setTablePrefix("t_");  // 表前缀
        strategy.setInclude(tableNames);
        mpg.setStrategy(strategy);

        // 模板引擎
        mpg.setTemplateEngine(new BeetlTemplateEngine());

        mpg.execute();
    }
}

